package com.fessc.develop.demos.web;

import com.fessc.develop.base.CCBResult;
import com.fessc.develop.base.Result;
import com.fessc.develop.entity.comvo.TaskJobVo;
import com.fessc.develop.service.CCBCallApiBaseService;
import com.fessc.develop.service.ComCallApiBaseService;
import com.fessc.develop.service.interfaces.ITBatchTimerDefService;
import com.fessc.develop.utils.BusiUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Controller
@RequestMapping("/fessc")
public class BasicController {

    @Resource
    private ITBatchTimerDefService batchTimerDefService;

    @Resource
    private ComCallApiBaseService baseService;

    @Resource
    private CCBCallApiBaseService ccbCallApiBaseService;

    /**
     * 外部系统通用回调访问接口
     * 主要功能：
     * 1、预付款、退款、跨境付汇等交易状态回调
     * 2、商铺、边民备案等信息查询
     */
    @PostMapping("/comcallapi")
    @ResponseBody
    public Result comcallapi(@RequestBody String requestBody) {
        //设置日志打印唯一编号lodId
        MDC.put("lodId", BusiUtil.getMDCLogId("", 15));
        return baseService.process(requestBody);
    }

    /**
     * 建行回调访问接口
     * 主要功能：
     * 1、预付款、退款、跨境付汇等交易状态回调
     * 2、商铺、边民备案等信息查询
     */
    @PostMapping("/ccbcallapi")
    @ResponseBody
    public CCBResult ccbcallapi(@RequestBody String requestBody) {
        //设置日志打印唯一编号lodId
        MDC.put("lodId", BusiUtil.getMDCLogId("", 15));
        return ccbCallApiBaseService.process(requestBody);
    }

    /**
     * 定时任务操作
     * 主要功能：
     * 定时任务启动、停止、手工执行
     */
    @PostMapping("/job")
    @ResponseBody
    public Result job(@RequestBody TaskJobVo requestBody) {
        //设置日志打印唯一编号lodId
        MDC.put("lodId", BusiUtil.getMDCLogId("", 15));
        log.info("定时任务操作类型（1-启动；2-停止；3-手动执行）: " + requestBody.getOption() + "-" + requestBody.getTaskCode());
        if (BusiUtil.isEquals(requestBody.getOption(), "1")) {
            //启动定时任务
            batchTimerDefService.startCron(requestBody.getTaskCode());
        } else if (BusiUtil.isEquals(requestBody.getOption(), "2")) {
            //停止定时任务
            batchTimerDefService.stopCron(requestBody.getTaskCode());
        } else if (BusiUtil.isEquals(requestBody.getOption(), "3")) {
            //手动执行定时任务
            batchTimerDefService.manuallyInvoke(requestBody.getTaskCode());
        }
        return new Result();
    }

}
